package com.pengshun.modules.api.user.controller;

import com.pengshun.modules.api.coal.validator.First;
import com.pengshun.modules.api.coal.validator.Second;
import com.pengshun.modules.api.user.service.SystemMessageService;
import com.pengshun.modules.api.user.service.dto.SystemMessageDto;
import com.pengshun.modules.common.BaseResult;
import com.pengshun.modules.domain.User;
import com.pengshun.modules.security.Authorization;
import com.pengshun.modules.security.CurrUserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author luoligang
 * @create 2021/3/12
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "消息相关")
@RequestMapping("/api/system/message")
@ApiIgnore
public class SystemMessageController {

    private final SystemMessageService systemMessageService;

    @ApiOperation(value = "消息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "size", value = "条数", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "categoryId", value = "类别id ", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    })
    @GetMapping(value = "/list", produces = "application/json; charset=utf-8")
    @Authorization
    public BaseResult list(@ApiIgnore @CurrUserInfo User user, @ApiIgnore @Validated(value = {First.class}) SystemMessageDto para) throws Exception {
        para.setUserId(user.getId());
        BaseResult res = systemMessageService.getMessageList(para);
        return res;
    }


    @ApiOperation(value = "消息详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "systemMessageId", value = "消息id ", required = true, dataType = "int", paramType = "query"),
    })
    @GetMapping(value = "/detil", produces = "application/json; charset=utf-8")
    public BaseResult detil(@ApiIgnore @Validated(value = {Second.class}) SystemMessageDto para) throws Exception {
        BaseResult res = systemMessageService.getMessageDetil(para);
        return res;
    }

    @ApiOperation(value = "查询消息类别")
    @Authorization
    @GetMapping(value = "/category", produces = "application/json; charset=utf-8")
    public BaseResult category(@ApiIgnore @CurrUserInfo User user) throws Exception {
        BaseResult res = systemMessageService.getMessageCategory(user.getId());
        return res;
    }
    @ApiOperation(value = "查询消息未读总数量")
    @Authorization
    @GetMapping(value = "/num", produces = "application/json; charset=utf-8")
    public BaseResult num(@ApiIgnore @CurrUserInfo User user) throws Exception {
        BaseResult res = systemMessageService.getNotReadMessageNum(user.getId());
        return res;
    }
    @ApiOperation(value = "更新消息为已读")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "systemMessageId", value = "消息id ", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "categoryId", value = "类别id ", required = true, dataType = "int", paramType = "query"),
    })
    @PostMapping(value = "/update", produces = "application/json; charset=utf-8")
    @Authorization
    public BaseResult update(@ApiIgnore @CurrUserInfo User user,@ApiIgnore SystemMessageDto para) throws Exception {
        para.setUserId(user.getId());
        BaseResult res = systemMessageService.updateMessageStatus(para);
        return res;
    }
}
